package com.sdkj.action.sys;

import java.io.File;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.sdkj.Entity.service.News;
import com.sdkj.Entity.sys.Images;
import com.sdkj.action.base.BaseAction;
import com.sdkj.service.sys.IImagesService;
import com.sdkj.utils.CommonUtils;
import com.sdkj.utils.PageBean;

@Controller
@Scope("prototype")
public class ImagesAction extends BaseAction<Images, IImagesService> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	

	/**
	 * 图片上传
	 * @author lijian
	 *
	 * @return
	 */
	public void saveFile() {
		// 获取上传是要传到哪的路径
		ServletContext sc = ServletActionContext.getServletContext();
		String path = sc.getRealPath("/upload"); 
		// 根据文件的真实名字创建一个文件 
		File dest = new File(path, fileFileName);
		JSONObject j=new JSONObject();
		try {
			int no=Integer.parseInt(sc.getAttribute("imgSortNo")+"");
			sc.setAttribute("imgSortNo", no+1); 
			// 将临时文件拷贝进创建的文件中
			FileUtils.copyFile(file, dest);
			Images img=new Images(); 
			img.setImgUrl("/upload/"+fileFileName);
			img.setType(model.getType());
			img.setSortNo(no);
			img.setImgAlt(model.getImgAlt());
			img.setImgHref(model.getImgHref());
			img.setImgTitle(model.getImgTitle());
			j.put("imgid", service.save(img));
			j.put(RES_CODE, 100);
			j.put(RES_MSG, "上传文件成功");
		} catch (Exception e) { 
			j.put(RES_CODE, -100);
			j.put(RES_MSG, "上传文件失败");
			e.printStackTrace();
		}
		// 删除临时文件
		file.delete(); 
		CommonUtils.writeJson(j);
		
	}
	
	public void list() throws SQLException{  
		String hql=" from Images where type=:t";
		
		Map<String,Object>params=new HashMap<String,Object>();
		//1公司动态 2 行业动态
		params.put("t", model.getType());
		hql+=" order by createTime desc";
		List<Images>data=service.find(hql, params, page, rows);
		JSONObject res=new JSONObject();
		res.put(RES_CODE, 100);
		res.put(RES_MSG, "查询成功");
		res.put("data", JSONArray.fromObject(data));
		CommonUtils.writeJson(res);
	}
	
	public void del() throws SQLException{
		boolean flag=service.deleteIds(ids);
		JSONObject res=new JSONObject();
		if(flag){
			res.put(RES_CODE, 100);
			res.put(RES_MSG, "删除数据成功");
		}else{
			res.put(RES_CODE, -100); 
			res.put(RES_MSG, "删除数据失败");
		}
		try {
			operationLogService.save(getOperationLogBean("delete img"));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		CommonUtils.writeJson(res);
	}
	
	/************************************************************/
	
	 //上传文件时的临时文件
	 private File file;
	 //上传文件的类型
	 private String fileContentType;
	 //上传文件的真实名称
	 private String fileFileName;

	public File getFile() {
		return file;
	}
	public void setFile(File file) {
		this.file = file;
	}
	public String getFileContentType() {
		return fileContentType;
	}
	public void setFileContentType(String fileContentType) {
		this.fileContentType = fileContentType;
	}
	public String getFileFileName() {
		return fileFileName;
	}
	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}


}
